package com.egg.ylt.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.adapter.ADA_GrowthDiaryList;
import com.egg.ylt.pojo.DiaryPhotoEntity;
import com.egg.ylt.presenter.impl.GrowthDiaryPerImpl;
import com.egg.ylt.view.IGrowthDiaryView;
import com.yonyou.framework.library.base.BaseFragment;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.common.utils.ToastUtil;
import com.yonyou.framework.library.eventbus.EventCenter;

import butterknife.BindView;

/**
 * Created by sichaoqi on 2018/9/3.
 */

public class FRA_GrowthDiary extends BaseFragment<GrowthDiaryPerImpl> implements IGrowthDiaryView {

    @BindView(R.id.growth_recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.ll_growth_loading_view)
    LinearLayout llGrowthLoadingView;

    private ADA_GrowthDiaryList ada_growthDiaryList;

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (ada_growthDiaryList != null){
            ada_growthDiaryList.setClearBtn();
        }
    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected View getLoadingTargetView() {
        return llGrowthLoadingView;
    }

    public void againLoadPhoto(){
        if (mPresenter != null){
            //请求照片数据
            mPresenter.getDiaryData(Constants.TOKEN, Constants.COMPANYID);
        }
    }



    @Override
    protected void initViewsAndEvents() {

        //设置recyclerview布局管理器和适配器
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        ada_growthDiaryList = new ADA_GrowthDiaryList(mContext);
        recyclerView.setAdapter(ada_growthDiaryList);

        //请求照片数据
        mPresenter.getDiaryData(Constants.TOKEN, Constants.COMPANYID);

        //删除照片
        ada_growthDiaryList.setOnItemCancelClickListener(new ADA_GrowthDiaryList.OnItemCancelClickListener() {
            @Override
            public void onItemClick(String text) {
                if (StringUtil.isEmpty(text)){
                    ToastUtil.makeText(mContext, "请选择图片 ", false);
                    return;
                }
                mPresenter.postDeleteDiary(Constants.TOKEN, Constants.COMPANYID, text);
            }
        });
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fra_growth_diary;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    public void getGrowthDiaryData(DiaryPhotoEntity entity) {
        if (entity == null){
            return;
        }
        if (ListUtil.isListEmpty(entity.getList())){
//            toggleShowEmpty(true, R.drawable.empty_no_response, "空空如也~");
            ada_growthDiaryList.setDataClear();
            return;
        }
        toggleShowEmpty(false, -1, "");
        ada_growthDiaryList.setDataList(entity.getList());
    }

    @Override
    public void onDeleteDiarySucceed() {
        ToastUtil.makeText(mContext, "删除成功", false);
        //请求照片数据
        mPresenter.getDiaryData(Constants.TOKEN, Constants.COMPANYID);
    }
}
